package yangyongchao.com.howtousefastjson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import yangyongchao.com.howtousefastjson.http.HttpPostFormData;
import yangyongchao.com.howtousefastjson.http.HttpResponse;
import yangyongchao.com.howtousefastjson.http.MyOkHttpClient;
import yangyongchao.com.howtousefastjson.http.ResponseHandler;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = getClass().getName();
    private String mStrUrl = "http://www.kuaidi100.com/query?type=yuantong&postid=11111111111";
    private Button mButton;
    private ArrayList<DataBean> mDataBeanList;
    private DataBean mBean;
    private MyAdapter mMyAdapter;
    private ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = findViewById(R.id.button);
        mListView = findViewById(R.id.list);
        mButton.setOnClickListener(this);
        netWork();
    }

    private void netWork() {
        final MyOkHttpClient request = new MyOkHttpClient();
        HttpPostFormData formData = new HttpPostFormData();
        formData.addFormData("postid", "11111111111");
        request.get(mStrUrl, null, new ResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                Log.d(TAG, "onFailure: "+e.getMessage());
            }

            @Override
            public void onSuccess(HttpResponse response) {
                String str = response.getBody().toString();
                Log.d(TAG, "onSuccess: "+str);
                getJsonObject(JSON.parseObject(str));
            }
        });

    }
    private void getJsonObject(JSONObject jsonObject){
        JSONArray array = jsonObject.getJSONArray("data");
        if(array.size()>0){
            mDataBeanList = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                mBean = new DataBean();
                JSONObject object = (JSONObject) array.get(i);
                mBean.setFtime(object.getString("ftime"));
                mBean.setTime(object.getString("time"));
                mBean.setLocation(object.getString("context"));
                Log.e(TAG, "ok"+mBean.toString());
                mDataBeanList.add(mBean);
            }
            mMyAdapter = new MyAdapter(mDataBeanList,this);
            mListView.setAdapter(mMyAdapter);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button){
            netWork();
        }
    }
    //    Callback callback = new Callback() {
//        @Override
//        public void onFailure(Call call, IOException e) {
//        }
//        @Override
//        public void onResponse(Call call, Response response) throws IOException {
//            String Str = response.body().string();
//            Log.e(TAG, "ok"+Str);
//            getJsonObject(JSON.parseObject(Str));
//        }
//    };
}
